package org.example.Demo01;

import org.example.Join.JDBC_Join;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class JDBC_Add {
    public JDBC_Add() {
    }
    private JDBC_Join jdbcJoin = new JDBC_Join();

    public int insert() throws Exception {
        //建立数据库连接
        Connection conn = jdbcJoin.join();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加的brandName：");
        String brandName = sc.next();
        System.out.println("请输入要添加的companyName：");
        String companyName = sc.next();
        System.out.println("请输入要添加的ordered：");
        int ordered = sc.nextInt();
        System.out.println("请输入要添加的description：");
        String description = sc.next();
        System.out.println("请输入要添加的status：");
        int status = sc.nextInt();

        String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status)" +
                " VALUES (?,?,?,?,?)";

        //这一部分好像也可以抽离出来，
        PreparedStatement prepstmt = conn.prepareStatement(sql);
        prepstmt.setString(1,brandName);
        prepstmt.setString(2,companyName);
        prepstmt.setInt(3,ordered);
        prepstmt.setString(4,description);
        prepstmt.setInt(5,status);

        //count是受影响的行数
        int count = prepstmt.executeUpdate();
        return count;
    }
}
